package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Warehouse;
import com.ericom.service.GenericManager;

public class WarehouseAction extends BaseAction {
	private GenericManager<Warehouse, Long> warehouseManager;
	private List warehouses;
	private Warehouse warehouse;
	private Long id;

	public Warehouse getWarehouse() {
		return warehouse;
	}

	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setWarehouseManager(
			GenericManager<Warehouse, Long> warehouseManager) {
		this.warehouseManager = warehouseManager;
	}

	public String list() {
		warehouses = warehouseManager.getAll();
		return SUCCESS;
	}

	public List getWarehouses() {
		return warehouses;
	}

	public String delete() {
		warehouseManager.remove(warehouse.getId());
		this.saveMessage(getText("warehouse.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			warehouse = warehouseManager.get(id);
		} else {
			warehouse = new Warehouse();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (warehouse.getId() == null);
		warehouse = warehouseManager.save(warehouse);

		String key = (isNew) ? "warehouse.added" : "warehouse.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
